# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=bison
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.4
pkgrel=2
pkgdesc="The GNU general-purpose parser generator"
arch=('any')
license=('GPL3')
url="https://www.gnu.org/software/bison/bison.html"
depends=("${MINGW_PACKAGE_PREFIX}-m4")
options=('debug' '!strip')
source=(https://ftp.gnu.org/gnu/bison/${_realname}-${pkgver}.tar.xz
        0001-use-gnu_printf.patch
        0002-add-relocation-support.patch
        0003-create_pipe-uses-O_TEXT-not-O_BINARY-mode.patch
        0004-open-source-file-in-binary-mode-MS-ftell-bug-ks-68337.patch)
sha256sums=('a72428c7917bdf9fa93cb8181c971b6e22834125848cf1d03ce10b1bb0716fe1'
            '3c29807364b20418b9de2166790fb2c9e46faf4959a78a78b5e4ccf8cd836147'
            'b3493cff8e697d3a1e7c2a545ab1b9adeccc24539b16efae46befc4083afca27'
            '3722461df2f48f8829f5deb307122b34321f88fdccb5ffb10112fd38231bf4ae'
            'b729a0b76e69a948eaaa2c59409520cbac9aba31f659537e88a69ba5e0720420')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  [ -f src/pathtools.h ] && rm -rf src/pathtools.h
  [ -f src/pathtools.c ] && rm -rf src/pathtools.c

  patch -p1 -i "${srcdir}"/0001-use-gnu_printf.patch
  patch -p1 -i "${srcdir}"/0002-add-relocation-support.patch
  patch -p1 -i "${srcdir}"/0003-create_pipe-uses-O_TEXT-not-O_BINARY-mode.patch
  patch -p1 -i "${srcdir}"/0004-open-source-file-in-binary-mode-MS-ftell-bug-ks-68337.patch

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}"/build-${CARCH}
  cd "${srcdir}"/build-${CARCH}
  M4=${MINGW_PREFIX}/bin/m4.exe \
    ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST}
  make
}

# Need to investigate all the testsuite failures.
#
# ERROR: 413 tests were run,
# 79 failed unexpectedly.
# 24 tests were skipped
#
# I checked two and they seemed to
# be down to IO problems in MSYS2.
#check() {
#  cd ${srcdir}/${_realname}-${pkgver}
#  rm -f tests/testsuite
#  make tests/testsuite
#  make check
#}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install
}
